package com.antis.olsl.magic.profit.cost;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.antis.olsl.base.BaseFragment;
import com.antis.olsl.magic.databinding.FragmentCostGrossProfitBinding;
import com.antis.olsl.magic.response.GetLevel5ProfitCostAnalysisResp;
import com.trello.rxlifecycle2.LifecycleTransformer;

import java.util.HashMap;
import java.util.Map;

import timber.log.Timber;

/**
 * 成本分析-毛利润Fragment
 */
public class CostGrossProfitFragment extends BaseFragment<FragmentCostGrossProfitBinding> implements CostGrossProfitContract.View {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private static final String ARG_PARAM3 = "param3";
    private static final String ARG_PARAM4 = "param4";

    private String mSalesroomId;
    private String mStartTime;
    private String mEndTime;
    private int mType;

    private CostGrossProfitPresenter mPresenter;

    private boolean isLoadData = false;//数据是否加载

    public CostGrossProfitFragment() {
    }

    public static CostGrossProfitFragment newInstance(String salesroomId, String startTime, String endTime, int type) {
        CostGrossProfitFragment fragment = new CostGrossProfitFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, salesroomId);
        args.putString(ARG_PARAM2, startTime);
        args.putString(ARG_PARAM3, endTime);
        args.putInt(ARG_PARAM4, type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mSalesroomId = getArguments().getString(ARG_PARAM1);
            mStartTime = getArguments().getString(ARG_PARAM2);
            mEndTime = getArguments().getString(ARG_PARAM3);
            mType = getArguments().getInt(ARG_PARAM4);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != mPresenter) {
            mPresenter.unSubscribe();
        }
    }

    @Override
    protected FragmentCostGrossProfitBinding getViewBinding() {
        return FragmentCostGrossProfitBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }

    @Override
    protected void initView(View view) {
        initPresenter();
    }

    private void initPresenter() {
        mPresenter = new CostGrossProfitPresenter();
        mPresenter.takeView(this);
    }

    @Override
    protected void loadData() {
        loadData(mSalesroomId, mStartTime, mEndTime, mType);
        Timber.tag("hhh").e("CostGrossProfitFragment loadData type= " + mType);
    }

    public void loadData(String salesroomId, String startTime, String endTime, int type) {
        if (!isLoadData) {
            mSalesroomId = salesroomId;
            mStartTime = startTime;
            mEndTime = endTime;
            mType = type;
            isLoadData = true;
            Map<String, Object> map = new HashMap<>(4);
            map.put("shopId", salesroomId);
            map.put("startTime", startTime);
            map.put("endTime", endTime);
            map.put("type", type);
            mPresenter.getLevel5ProfitCostAnalysis(map);
        }
    }

    @Override
    public void getLevel5ProfitCostAnalysisSuccess(GetLevel5ProfitCostAnalysisResp.ContentBean bean) {
        isLoadData = false;
        setTopInfo(bean);

        setContentInfo(bean);
    }

    private void setTopInfo(GetLevel5ProfitCostAnalysisResp.ContentBean bean) {
        // sku数
        if (!TextUtils.isEmpty(bean.skuNumber)) {
            viewBinding.textSkuNum.setText(bean.skuNumber);
        } else {
            viewBinding.textSkuNum.setText("0");
        }

        // 区间值
        if (!TextUtils.isEmpty(bean.intervalValue)) {
            viewBinding.textIntervalValue.setText(bean.intervalValue);
        } else {
            viewBinding.textIntervalValue.setText("-");
        }

        // 占比
        if (!TextUtils.isEmpty(bean.proportion)) {
            viewBinding.textProportion.setText(bean.proportion);
        } else {
            viewBinding.textProportion.setText("-");
        }

        // 平均毛利率
        if (!TextUtils.isEmpty(bean.averageGrossMargin)) {
            viewBinding.textAverage.setText(bean.averageGrossMargin);
        } else {
            viewBinding.textAverage.setText("-");
        }
    }

    private void setContentInfo(GetLevel5ProfitCostAnalysisResp.ContentBean bean) {
        // 边际成本
        if (!TextUtils.isEmpty(bean.marginCostProportion)) {
            viewBinding.textMarginalCostProportion.setText(bean.marginCostProportion);
        } else {
            viewBinding.textMarginalCostProportion.setText("-");
        }

        if (!TextUtils.isEmpty(bean.marginCost)) {
            viewBinding.textMarginalCost.setText(bean.marginCost);
        } else {
            viewBinding.textMarginalCost.setText("0");
        }

        // 促销成本
        if (!TextUtils.isEmpty(bean.promotionCostProportion)) {
            viewBinding.textPromotionCostProportion.setText(bean.promotionCostProportion);
        } else {
            viewBinding.textPromotionCostProportion.setText("-");
        }

        if (!TextUtils.isEmpty(bean.promotionCost)) {
            viewBinding.textPromotionCost.setText(bean.promotionCost);
        } else {
            viewBinding.textPromotionCost.setText("0");
        }

        // 产品成本
        if (!TextUtils.isEmpty(bean.productCostProportion)) {
            viewBinding.textProductCostProportion.setText(bean.productCostProportion);
        } else {
            viewBinding.textProductCostProportion.setText("-");
        }

        if (!TextUtils.isEmpty(bean.productCost)) {
            viewBinding.textProductCost.setText(bean.productCost);
        } else {
            viewBinding.textProductCost.setText("0");
        }

        // 残损成本
        if (!TextUtils.isEmpty(bean.damagedCostProportion)) {
            viewBinding.textDamageCostProportion.setText(bean.damagedCostProportion);
        } else {
            viewBinding.textDamageCostProportion.setText("-");
        }

        if (!TextUtils.isEmpty(bean.damagedCost)) {
            viewBinding.textDamageCost.setText(bean.damagedCost);
        } else {
            viewBinding.textDamageCost.setText("0");
        }

        // 产出比
        if (!TextUtils.isEmpty(bean.outputRatio)) {
            viewBinding.textOutputRatio.setText(bean.outputRatio);
        } else {
            viewBinding.textOutputRatio.setText("-");
        }

        // 周转资金
        if (!TextUtils.isEmpty(bean.workingCapital)) {
            viewBinding.textWorkingCapital.setText(bean.workingCapital);
        } else {
            viewBinding.textWorkingCapital.setText("0");
        }

        // 合计成本率
        if (!TextUtils.isEmpty(bean.costRateTotal)) {
            viewBinding.textCostRate.setText(bean.costRateTotal);
        } else {
            viewBinding.textCostRate.setText("-");
        }
    }

    @Override
    public void getLevel5ProfitCostAnalysisFailure(String msg) {
        isLoadData = false;
        setDefaultValue();
    }

    private void setDefaultValue() {
        viewBinding.textSkuNum.setText("0");
        viewBinding.textIntervalValue.setText("-");
        viewBinding.textProportion.setText("-");
        viewBinding.textAverage.setText("-");
        // 边际成本
        viewBinding.textMarginalCostProportion.setText("-");
        viewBinding.textMarginalCost.setText("0");
        // 促销成本
        viewBinding.textPromotionCostProportion.setText("-");
        viewBinding.textPromotionCost.setText("0");
        // 产品成本
        viewBinding.textProductCostProportion.setText("-");
        viewBinding.textProductCost.setText("0");
        // 残损成本
        viewBinding.textDamageCostProportion.setText("-");
        viewBinding.textDamageCost.setText("0");
        // 产出比
        viewBinding.textOutputRatio.setText("-");
        // 资金周转
        viewBinding.textWorkingCapital.setText("0");
        // 合计成本率
        viewBinding.textCostRate.setText("-");
    }

    @Override
    public boolean isActive() {
        return false;
    }

    @Override
    public <T> LifecycleTransformer<T> bindToLife() {
        return this.bindToLifecycle();
    }
}
